package com.androider.timememory.activity;

import java.util.ArrayList;
import java.util.HashMap;

import com.androider.timememory.R;
import com.androider.timememory.db.RecordModel;
import com.androider.timememory.utils.Constants;
import com.androider.timememory.utils.Dict;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.Phones;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class WhiteList extends Activity {
	final static private String TAG = "WhiteList";
	private Context mContext;
	private GridView whiteListGrid;
	private Button addBigBtn;
	private SimpleAdapter adapter;
	private ArrayList<HashMap<String, Object>> mData;
	private UpdateUIReceiver receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.white_list_layout);
		mContext = this;
		
		init();
		setOnListeners();
		
		receiver = new UpdateUIReceiver();
		IntentFilter filter = new IntentFilter();
        filter.addAction(Constants.ACTION_WHITELIST_UPDATE);
        registerReceiver(receiver, filter);
	}
	
	@Override
	protected void onDestroy() {
		unregisterReceiver(receiver);
		super.onDestroy();
	}

	private void init() {
		whiteListGrid = (GridView) findViewById(R.id.white_list_grid);
		addBigBtn = (Button) findViewById(R.id.add_btn);
		
		addBigBtn.setText("添加");
		findViewById(R.id.search).setVisibility(View.GONE);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(R.string.title_white_list);
		
		mData = RecordModel.getWhiteList(mContext);
		adapter = new SimpleAdapter(mContext, mData, R.layout.white_list_item,
				new String[] { Dict.C_name, Dict.C_number }, 
				new int[] { R.id.item_name, R.id.item_number });
		
		whiteListGrid.setAdapter(adapter);
	}
	
	private void setOnListeners() {
		/*addBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(mContext, AddPeople.class));
			}
			
		});*/
		
		addBigBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(mContext, AddPeople.class));
//				Intent i = new Intent(Intent.ACTION_PICK);
//                i.setType("vnd.android.cursor.dir/phone");
//                startActivityForResult(i, 0);
			}
			
		});
	}
	
	/******************** Receiver **********************/
    public class UpdateUIReceiver extends BroadcastReceiver {
    	
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if(action.equals(Constants.ACTION_WHITELIST_UPDATE)) {
				RecordModel.updateDataList(mData, RecordModel.getWhiteList(mContext));
				adapter.notifyDataSetChanged();
			}
			
		}
    }
    
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        switch (requestCode) {
            case 0:
                if (data == null) {
                    return;
                }
                Uri uri = data.getData();
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                cursor.moveToFirst();
                
                String number = cursor.getString(cursor.getColumnIndexOrThrow(Phones.NUMBER));
                
                Log.d(TAG, "number" + number);
                
                break;
            
            default:
                break;
        }
    }
}
